八、SpringBootWeb开发 您所在的位置:网站首页 springbootweb开发 html 八、SpringBootWeb开发

八、SpringBootWeb开发

2024-07-16 08:57| 来源: 网络整理| 查看: 265

由于 SpringBoot 使用了嵌入式 Servlet 容器。所以 JSP 默认是不能使用的。 如果需要服务端页面渲染,优先考虑使用模板引擎。

image

模板引擎页面默认放在 src/main/resources/templates

SpringBoot 包含以下模板引擎的自动配置

FreeMarker Groovy Thymeleaf Mustache

Thymeleaf官网:https://www.thymeleaf.org/

DOCTYPE html> Good Thymes Virtual Grocery Welcome to our grocery store! 原内容 登录 1234

表达式:用来动态取值

${}:变量取值;使用model共享给页面的值都直接用${}

**@{}:url路径;**可以自动匹配项目路径,防止项目路径改变后需要手动修改

1234

#{}:国际化消息

~{}:片段引用

*{}:变量选择:需要配合th:object绑定对象

系统工具&内置对象:详细文档 (opens new window)

param:请求参数对象 session:session对象 application:application对象 #execInfo:模板执行信息 #messages:国际化消息 #uris:uri/url工具 #conversions:类型转换工具 #dates:日期工具,是java.util.Date对象的工具类 #calendars:类似#dates,只不过是java.util.Calendar对象的工具类 #temporals: JDK8+ **java.time** API 工具类 #numbers:数字操作工具 #strings:字符串操作 #objects:对象操作 #bools:bool操作 #arrays:array工具 #lists:list工具 #sets:set工具 #maps:map工具 #aggregates:集合聚合工具(sum、avg) #ids:id生成工具

例如:

转大写 12# 2. 语法示例

表达式:

变量取值:${...} url 取值:@{...} 国际化消息:#{...} 变量选择:*{...} 片段引用: ~{...}

常见:

文本: 'one text','another one!',... 数字: 0,34,3.0,12.3,... 布尔:true、false null: null 变量名: one,sometext,main...

文本操作:

拼串: + 文本替换:| The name is ${name} | 拼串 123

布尔操作:

二进制运算: and,or 取反:!,not

比较运算:

比较:>,原内容 登录 1234# 4. 遍历

语法: th:each="元素名,迭代状态 : ${集合}"

Onions 2.41 yes Onions 2.41 yes 1234567891011

iterStat 有以下属性:

index:当前遍历元素的索引,从0开始 count:当前遍历元素的索引,从1开始 size:需要遍历元素的总数量 current:当前正在遍历的元素对象 even/odd:是否偶数/奇数行 first:是否第一个元素 last:是否最后一个元素 # 5. 判断 # th:if view User is an administrator User is a manager User is some other thing 12345# 6. 属性优先级 片段 遍历 判断 Item description here... 123

以下优先级从高到低排序

Order Feature Attributes 1 片段包含 th:insert th:replace 2 遍历 th:each 3 判断 th:if th:unless th:switch th:case 4 定义本地变量 th:object th:with 5 通用方式属性修改 th:attr th:attrprepend th:attrappend 6 指定属性修改 th:value th:href th:src ... 7 文本值 th:text th:utext 8 片段指定 th:fragment 9 片段移除 th:remove # 7. 行内写法 [[...]] or [(...)]

Hello, [[${session.user.name}]]!

12# 8. 变量选择

可以用th:object绑定一个对象,下面使用 * 就可以快捷使用

Name: Sebastian. Surname: Pepper. Nationality: Saturn. 12345

等同于

Name: Sebastian. Surname: Pepper. Nationality: Saturn. © 2011 The Good Thymes Virtual Grocery 结果: © 2011 The Good Thymes Virtual Grocery © 2011 The Good Thymes Virtual Grocery 1234567891011121314151617# 10. devtools org.springframework.boot spring-boot-devtools 1234

修改页面后;ctrl+F9刷新效果;

java代码的修改,如果devtools热启动了,可能会引起一些bug,难以排查



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有